from django.db import models

from apps.accounts.models import User
from apps.projectbases.models import ProjectPlanHModel
from bases.model import APIModel


class ProjectQuotaModel(APIModel):
    QUOTE_TYPES = (('M', '维护'), ('D', '开发'))
    projectPlanFK = models.ForeignKey(ProjectPlanHModel, on_delete=models.DO_NOTHING)
    quote_no = models.CharField(verbose_name='报价单号', max_length=10, blank=True, null=True)
    quote_wfno = models.CharField(verbose_name='报价WFNO', max_length=10, blank=True, null=True)
    quote_curr = models.CharField(verbose_name='报价币种', max_length=3)
    quote_date = models.DateField(verbose_name='报价日期', blank=True, null=True)
    quote_content = models.CharField(verbose_name='报价内容', max_length=50, blank=True, null=True)
    quote_amount = models.DecimalField(verbose_name='报价金额', decimal_places=6, max_digits=18, blank=True, null=True)
    quote_days = models.DecimalField(verbose_name='报价天数', decimal_places=6, max_digits=18, blank=True, null=True)
    quote_type = models.CharField(verbose_name='报价类型', max_length=1, blank=True, null=True, choices=QUOTE_TYPES)
    receive_date = models.DateField(verbose_name='受注日', blank=True, null=True)
    note = models.CharField(verbose_name='备注', max_length=255, blank=True, null=True)

    class Meta:
        db_table = 'PROJECTQUOTE'
        verbose_name = '报价主表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.quote_no


class ProjectMaintainModel(APIModel):
    projectPlanFK = models.ForeignKey(ProjectPlanHModel, on_delete=models.DO_NOTHING, related_name='maintain_projects')
    project_name = models.CharField(verbose_name='项目名称', max_length=50, blank=True, null=True)
    project_status = models.CharField(verbose_name='项目状态', max_length=1, blank=True, null=True, default='O')
    quoteFK = models.ForeignKey(ProjectQuotaModel, related_name='quota_maintain_relations', on_delete=models.DO_NOTHING)
    order_no = models.CharField(verbose_name='订单号', max_length=10, blank=True, null=True)
    sales_date = models.DateField(verbose_name='预定销售年月')
    sales_amount = models.DecimalField(verbose_name='预定销售金额', max_digits=18, decimal_places=6,  blank=True, null=True)
    procurement_obj = models.DecimalField(verbose_name='采购管理对象', max_digits=18, decimal_places=6, blank=True, null=True)
    procurement_affect = models.DecimalField(verbose_name='采购管理事务', max_digits=18, decimal_places=6, blank=True, null=True,default=0)
    Maintain_estimates = models.DecimalField(verbose_name='维护预估金额', max_digits=18, decimal_places=6, blank=True, null=True,default=0)
    quoted_days = models.DecimalField(verbose_name='报价工数（人日）', max_digits=18, decimal_places=6, blank=True, null=True)
    quoted_hours = models.DecimalField(verbose_name='报价工数（人时）', max_digits=18, decimal_places=6, blank=True, null=True)
    bugdget_hours = models.DecimalField(verbose_name='预算工数（人时）', max_digits=18, decimal_places=6, blank=True, null=True)
    reserve_hours = models.DecimalField(verbose_name='预算预备工数（人时）', max_digits=18, decimal_places=6, blank=True, null=True)
    note = models.CharField(verbose_name='备注', max_length=255, blank=True, null=True)


    class Meta:
        db_table = 'PROJECTMAINTAIN'
        verbose_name = '维护项目金额工时表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.project_name

class ProjectDevelopModel(APIModel):
    projectPlanFK = models.ForeignKey(ProjectPlanHModel, on_delete=models.DO_NOTHING, related_name='develop_projects')
    project_name = models.CharField(verbose_name='项目名称', max_length=50, blank=True, null=True)
    project_status = models.CharField(verbose_name='项目状态', max_length=1, blank=True, null=True, default='O')
    quoteFK = models.ForeignKey(ProjectQuotaModel, related_name='quota_develop_relations', on_delete=models.DO_NOTHING)
    order_no = models.CharField(verbose_name='订单号', max_length=10, blank=True, null=True)
    sales_date = models.DateField(verbose_name='预定销售年月')
    quote_amount = models.DecimalField(verbose_name='报价金额', max_digits=18, decimal_places=6, blank=True,null=True)
    sales_amount = models.DecimalField(verbose_name='预定销售金额', max_digits=18, decimal_places=6, blank=True,null=True)
    operating_profit = models.DecimalField(verbose_name='营业利润', max_digits=18, decimal_places=6, blank=True,null=True)
    operating_margin = models.DecimalField(verbose_name='营业利润率', max_digits=18, decimal_places=6, blank=True,null=True)
    reserve_amount = models.DecimalField(verbose_name='预备金额', max_digits=18, decimal_places=6, blank=True,null=True)
    quoted_days = models.DecimalField(verbose_name='报价工数（人日）', max_digits=18, decimal_places=6, blank=True, null=True)
    quoted_hours = models.DecimalField(verbose_name='报价工数（人时）', max_digits=18, decimal_places=6, blank=True, null=True)
    a1bugdget_hours = models.DecimalField(verbose_name='A1预算工数（人时）', max_digits=18, decimal_places=6, blank=True, null=True)
    a2manage_hours = models.DecimalField(verbose_name='A2管理工数（人时）', max_digits=18, decimal_places=6, blank=True,null=True)
    a3risk_hours = models.DecimalField(verbose_name='A3风险预备工数（人时）', max_digits=18, decimal_places=6, blank=True,null=True)
    note = models.CharField(verbose_name='备注', max_length=255, blank=True, null=True)

    class Meta:
        db_table = 'PROJECTDEVELOP'
        verbose_name = '开发项目金额工时表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.project_name

class ProjectMaintainAccountModel(APIModel):
    projectMaintainFK = models.ForeignKey(ProjectMaintainModel, on_delete=models.DO_NOTHING)
    sales_date = models.DateField(verbose_name='预定销售年月')
    sales_amount = models.DecimalField(verbose_name='营业额', max_digits=18, decimal_places=6, blank=True, null=True)
    amount = models.DecimalField(verbose_name='当前耗费', max_digits=18, decimal_places=6, blank=True, null=True)
    profit_loss = models.DecimalField(verbose_name='损益', max_digits=18, decimal_places=6, blank=True, null=True)
    note = models.CharField(verbose_name='备注', max_length=255, blank=True, null=True)

    class Meta:
        db_table = 'MAINTAINPROJECTACCOUNT'
        verbose_name = '维护项目核算履历表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.id


class ProjectDevelopAccountModel(APIModel):
    projectDevelopFK = models.ForeignKey(ProjectDevelopModel, on_delete=models.DO_NOTHING)
    sales_date = models.DateField(verbose_name='预定销售年月')
    sales_amount = models.DecimalField(verbose_name='营业额', max_digits=18, decimal_places=6, blank=True, null=True)
    amount = models.DecimalField(verbose_name='当前耗费', max_digits=18, decimal_places=6, blank=True, null=True)
    profit_loss = models.DecimalField(verbose_name='损益', max_digits=18, decimal_places=6, blank=True, null=True)
    note = models.CharField(verbose_name='备注', max_length=255, blank=True, null=True)

    class Meta:
        db_table = 'DEVELOPPROJECTACCOUNT'
        verbose_name = '开发项目核算履历表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.id


class ProjectMaintainStageModel(APIModel):
    projectMaintainFK = models.ForeignKey(ProjectMaintainModel, on_delete=models.DO_NOTHING)
    sales_date = models.DateField(verbose_name='预定销售年月')
    quoted_hours = models.DecimalField(verbose_name='报价工数', max_digits=18, decimal_places=6, blank=True, null=True)
    bugdget_hours = models.DecimalField(verbose_name='预算工数', max_digits=18, decimal_places=6, blank=True, null=True)
    actual_hours = models.DecimalField(verbose_name='实绩工数', max_digits=18, decimal_places=6, blank=True, null=True)
    margin_hours = models.DecimalField(verbose_name='工数计划差', max_digits=18, decimal_places=6, blank=True, null=True)
    note = models.CharField(verbose_name='备注', max_length=255, blank=True, null=True)

    class Meta:
        db_table = 'MAINTAINPROJECTSTAGE'
        verbose_name = '维护项目阶段别预实管理表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.id


class ProjectDevelopStageModel(APIModel):
    projectDevelopFK = models.ForeignKey(ProjectDevelopModel, on_delete=models.DO_NOTHING)
    sales_date = models.DateField(verbose_name='预定销售年月')
    quoted_hours = models.DecimalField(verbose_name='报价工数', max_digits=18, decimal_places=6, blank=True, null=True)
    bugdget_hours = models.DecimalField(verbose_name='预算工数', max_digits=18, decimal_places=6, blank=True, null=True)
    actual_hours = models.DecimalField(verbose_name='实绩工数', max_digits=18, decimal_places=6, blank=True, null=True)
    margin_hours = models.DecimalField(verbose_name='工数计划差', max_digits=18, decimal_places=6, blank=True, null=True)
    note = models.CharField(verbose_name='备注', max_length=255, blank=True, null=True)

    class Meta:
        db_table = 'DEVELOPPROJECTSTAGE'
        verbose_name = '开发项目阶段别预实管理表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.id


class ProjectProcessModel(APIModel):
    projectPlanFK = models.ForeignKey(ProjectPlanHModel, on_delete=models.DO_NOTHING)
    projectId = models.BigIntegerField(verbose_name='项目ID',blank=True,null=True)
    recorder = models.ForeignKey(User, verbose_name='记录者', related_name='+', on_delete=models.DO_NOTHING, blank=True, null=True)
    report_recipient = models.CharField(verbose_name='报告对象', max_length=50, blank=True, null=True)
    report_form = models.CharField(verbose_name='报告形态', max_length=50, blank=True, null=True)
    note = models.CharField(verbose_name='备注', max_length=255, blank=True, null=True)

    class Meta:
        db_table = 'PROJECTPROGRESS'
        verbose_name = '项目进度记录表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.id

# class ProjectRegimeModel(APIModel):
#     project_plan = models.ForeignKey(ProjectPlanHModel, on_delete=models.DO_NOTHING)
#     team_manageer = models.CharField(verbose_name='项目经理', max_length=30, blank=True, null=True)
#     sales_person = models.CharField(verbose_name='营业担当', max_length=30, blank=True, null=True)
#     team_leader = models.CharField(verbose_name='项目组长', max_length=30, blank=True, null=True)
#     designer = models.CharField(verbose_name='设计担当', max_length=30, blank=True, null=True)
#     quality_super = models.CharField(verbose_name='品质管理者', max_length=30, blank=True, null=True)
#     develop_super = models.CharField(verbose_name='开发担当', max_length=30, blank=True, null=True)
#     develop_users = models.CharField(verbose_name='开发人员', max_length=30, blank=True, null=True)
#
#
#     class Meta:
#         db_table = 'PROJECTREGIME'
#         verbose_name = '项目内部体制信息表'
#         verbose_name_plural = verbose_name
#
#     def __str__(self):
#         return self.team_manageer
#
#
# class ProjectCustomRegimeModel(APIModel):
#     project_plan = models.ForeignKey(ProjectPlanHModel, on_delete=models.DO_NOTHING)
#     representative = models.CharField(verbose_name='项目代表', max_length=30, blank=True, null=True)
#
#     class Meta:
#         db_table = 'PROJECTCUSTOMREGIME'
#         verbose_name = '项目客户体制信息表'
#         verbose_name_plural = verbose_name
#
#     def __str__(self):
#         return self.representative